@extends('layout.admin')
@section("content")

    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" style="padding-left: 0px;padding-right:0px;">
        <h1 class="page-header"><a href="/admin/lesson/create" type="button" class="btn btn-primary">新增课程</a></h1>
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th class="text-center">序号</th>
                        <th class="text-center">课程名称</th>
                        <th class="text-center">创建时间</th>
                        <th class="text-center">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                            if ($lessons) {
                                $i = ($lessons->currentPage() - 1) * $lessons->perPage() +1;
                                foreach ($lessons as $lesson) {
                    ?>
                                    <tr>
                                        <td class="text-center"><?php echo $i;?></td>
                                        <td class="text-center"><?php echo $lesson->name;?></td>
                                        <td class="text-center"><?php echo date('Y-m-d H:i:s',$lesson->created);?></td>
                                        <td class="text-center">
                                            <a href="/admin/lesson/create?id=<?php echo isset($lesson['id'])?$lesson['id']:'';?>" type="button" class="btn btn-primary">编辑</a>
                                            <button type="button" data-id="<?php echo isset($lesson['id'])?$lesson['id']:'';?>" class="btn btn-primary delete">删除</button>
                                        </td>
                                    </tr>
                    <?php
                            $i++;
                                }
                            }
                    ?>
                </tbody>
            </table>

            <input type="hidden" id = "_token" name="_token" value="<?php echo csrf_token(); ?>">
            <ul class="pagination pagination-lg">
                <?php echo $lessons->render();?>
            </ul>
        </div>
    </div>

    <script type="text/javascript">
        $(document).ready(function(){
            //删除
            $(".delete").click(function(){
                var id = $(this).attr('data-id');
                var _token = $("#_token").val();
                $.ajax({
                    url :'/admin/lesson/delete',
                    type:'POST',
                    data:'id='+id+'&_token='+_token,
                    dataType:'json',
                    success:function(data){
                        alert(data.msg);
                        window.location.reload();
                    }
                });
            });
        });
    </script>
@endsection